var express = require('express');
var router = express.Router();

router.get('/', function(req, res) {
	defaultRouter(req, res)
});
router.get('/:apitype', function(req, res) {
	if(req.params.apitype == "test") {
		apiTestRouter(req, res)
	}
});

module.exports = router;

function defaultRouter(req, res) {
	res.send('this is api.js');
}

function apiTestRouter(req, res) {
	var _callback = req.query.callback;
	var _data = {
		error: 0,
		data: {
			id: req.query.id,
			name: 'tom'
		}
	};
	if(_callback) {
		res.type('text/javascript');
		res.send(_callback + '(' + JSON.stringify(_data) + ')');
	} else {
		res.json(_data);
	}
}